package com.zhhe.linearList;

import java.util.Arrays;

/*
 *Author:ZouHeng
 *Des: 顺序表
 * 优点：遍历比较快
 * 缺点：插入和删除需要移动大量元素，代价比较大
 *Date:2020-12-08  10:09
 */
public class SeqList<T> implements IMyList<T> {
    /**
     * 顺序表最大容量
     */
    private int maxSize;

    /**
     * 顺序表当前容量
     */
    private int size;

    private T[] arrayList;

    public SeqList(int maxSize) {
        this.maxSize = maxSize;
        this.size = 0;
        arrayList = (T[])new Object[maxSize];
    }

    @Override
    public T get(int index) {
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引错误，无法删除元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return arrayList[index];
    }

    @Override
    public void add(T t) {
        if (size == maxSize) {
            try {
                throw new Exception("顺序表已满，无法插入数据");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        arrayList[size++] = t;
    }

    @Override
    public void add(int index, T t) {

        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法插入元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        for (int i = size; i > index; i--) {
            arrayList[i] = arrayList[i - 1];
        }
        arrayList[index] = t;
        size++;
    }

    @Override
    public void remove(int index) {
        if (size == 0) {
            try {
                throw new Exception("顺序表为空，无法删除元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法删除元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        for (int i = index; i < size; i++){
            arrayList[i] = arrayList[i + 1];
        }
        size--;
    }

    @Override
    public void removeAll() {
        for (int i = 0; i < size; i++) {
            arrayList[i] = null;
        }
        size = 0;
    }

    @Override
    public int size() {
        return this.size;
    }


    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public String toString() {
        return "linearList.SeqList{" + "maxSize=" + maxSize + ", size=" + size + ", arrayList=" + Arrays.toString(arrayList) + '}';
    }

    public static void main(String[] args) {
        SeqList<Integer> seqList = new SeqList<>(100);
        System.out.println("原始线性表：" + seqList.toString());
        //插入元素
        for (int i = 0; i < 10; i++) {
             seqList.add(i, i);
        }
        System.out.println("插入元素后的线性表：" + seqList.toString());
        seqList.remove(7);
        System.out.println("删除元素后的线性表：" + seqList.toString());
        for (int i = 0; i < seqList.size(); i++) {
            System.out.println("顺序表第" + i + "个元素为：" + seqList.get(i));
        }
    }

}
